//
//  MineHeadView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/8/21.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol MineHeadViewDelegate {
    @objc optional func selectService(index:Int) //选择操作类型
}

class MineHeadView: UIView {
    
    weak var mineDelegate : MineHeadViewDelegate?
    
    var redView = UIView() //红色View
    var headImageView = UIImageView() //头像
    var nameLabel = UILabel() //昵称
    
    fileprivate let cellId = "serviceCell"
    
    //服务模块
    var collectionView : UICollectionView!
    //服务模块数据
    var dataArray = [speedCard,myCardPackage,financialProduct,insuranceProducts]
    //服务模块图片
    var imageArray = [#imageLiteral(resourceName: "getcredit"),#imageLiteral(resourceName: "cardbag"),#imageLiteral(resourceName: "mangermoney"),#imageLiteral(resourceName: "insurance")]
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = UIColor.mineColor()
        
        redView.backgroundColor = UIColor.red
        redView.contentMode = .scaleAspectFill
        self.addSubview(redView)
        
//        let blurView = DRNRealTimeBlurView()
//        blurView.layer.cornerRadius = 0
//        blurView.layer.masksToBounds = true
//        blurView.alpha = 0.8
//        redView.addSubview(blurView)
        
        headImageView.layer.cornerRadius = RATIO_H(maxNum: 40)
        headImageView.layer.masksToBounds = true
        if KMTool.isSignIn() && KMTool.stringForEnumKey(.avatar) != "" {
            headImageView.image = KMTool.base64ToImage(base: KMTool.stringForEnumKey(.avatar)!)
            self.redView.layer.contents = KMTool.blurryImage(image: headImageView.image!).cgImage
            self.redView.contentMode = .scaleAspectFill
            self.redView.layer.masksToBounds = true
            nameLabel.text = KMTool.stringForEnumKey(.userName)
            self.redView.backgroundColor = UIColor.clear
        }else{
            headImageView.image = #imageLiteral(resourceName: "avatar_default")
            self.redView.layer.contents = nil
            self.redView.backgroundColor = UIColor.red
            
            if KMTool.isSignIn() {
                if KMTool.stringForEnumKey(.userName) != "" {
                    nameLabel.text = KMTool.stringForEnumKey(.userName)
                }else{
                    nameLabel.text = KMTool.stringForEnumKey(.phone)
                }
            }else{
                nameLabel.text = logInNow
            }
        }
        
        redView.addSubview(headImageView)
        
        
        nameLabel.textColor = UIColor.white
        nameLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        redView.addSubview(nameLabel)
        
        redView.snp.makeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 175))
        }
        
//        blurView.snp.makeConstraints { (make) in
//            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
//        }
        
        headImageView.snp.makeConstraints { (make) in
            
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(RATIO_H(maxNum: -10))
            make.width.height.equalTo(RATIO_H(maxNum: 80))
        }
        
        nameLabel.snp.makeConstraints { (make) in
        make.top.equalTo(headImageView.snp.bottom).offset(RATIO_H(maxNum: 15))
            make.centerX.equalTo(headImageView)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize.init(width: ScreenW/2, height: RATIO_H(maxNum: 85))
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.isScrollEnabled = false
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(ServiceCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.top.equalTo(redView.snp.bottom)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 170))
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension MineHeadView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? ServiceCell
        cell?.imageView.image = imageArray[indexPath.row]
        cell?.textLabel.text = dataArray[indexPath.row]
        
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.mineDelegate?.selectService!(index: indexPath.row)
    }
}








